﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;

namespace SKBKontur.Serialization.Binding
{
    public abstract class Binder : Attribute
    {
        protected MethodInfo GetPropertySetter(object o, string propertyName)
        {
            PropertyInfo propertyInfo = o.GetType().GetProperty(propertyName);
            if (propertyInfo == null)
                throw new SerializationException(string.Format("Property {0} cannot be resolved", propertyName));
            return propertyInfo.GetSetMethod();
        }

        protected static PropertyInfo GetPropertyInfo(object o, string propertyName)
        {
            PropertyInfo propertyInfo = o.GetType().GetProperty(propertyName);
            if (propertyInfo == null)
                throw new SerializationException(string.Format("Property {0} cannot be resolved", propertyName));
            return propertyInfo;
        }

        public abstract void Bind(ref object objectToBind, string propertyName,
                                  Dictionary<string, byte[]> serializedData);

        public abstract void Unbind(object objectToUnbind, string propertyName,
                                    ref Dictionary<string, byte[]> serializedData);

        public virtual bool IsLast()
        {
            return false;
        }

    }
}